<?php

date_default_timezone_set('Asia/Saigon');

class ShoppingController extends Controller {

    public $layout = '//layouts/shop';

    public function actionIndex() {


        $this->render('index');
    }

    public function actionHome($id) {
        $this->render('index', array('id' => $id));
    }

    public function actiondanhmuc($url) {
         $id = $_GET['id'];
        $shopcode = 'shop' . $id;
        if (isset(Yii::app()->request->cookies[$shopcode]->value)) {
            $shoparry = Yii::app()->request->cookies[$shopcode]->value;
            $savedCardArray = json_decode($shoparry, true);
        }
        $category = ShopNavbar::model()->find('shopid='.$savedCardArray[0].' and url="'.$url.'"');
        $this->pageTitle = $category->title;

        $category1 = ShopNavbar::model()->findAll(array(
            'condition' => 'flag=1 and parent= ' . $category->id,
        ));
        $sql = 'flag=1 AND cate_id in(' . $category->id;

        if (!empty($category1)) {
            foreach ($category1 as $item) {
                $sql = $sql . ',' . $item->id;
            }
            $sql = $sql . ')';
        } else {
            $sql = 'flag=1 AND cate_id=' . $category->id;
        }
        $sql = $sql . '  order by `position` ASC';
        $criteria = new CDbCriteria();
        $criteria->condition = $sql;
        //$criteria->order = 'rank DESC,id DESC';

        $count = ShopProduct::model()->count($criteria);
        $pages = new CPagination($count);
        //results per page
        $pages->pageSize = 30;
        $pages->applyLimit($criteria);
        $produc = ShopProduct::model()->findAll($criteria);

        $this->render('danh-muc', array(
            'produc' => $produc,
            'pages' => $pages,
            'category' => $category,
            'sql' => $sql,
        ));
    }

    public function actionDetail($k) {


        $produc = ShopProduct::model()->findByPk($k);
        $this->pageTitle = $produc->pr_name;
        Yii::app()->clientScript->registerMetaTag($produc->pr_meta, 'keywords');
        Yii::app()->clientScript->registerMetaTag($produc->pr_description, 'description');
        $criteria = new CDbCriteria();
        $criteria->condition = 'flag=1 AND cate_id=' . $produc->cate_id . ' AND prid<>' . $produc->prid . ' AND `pr_enddate` >= CURDATE( )';
        $criteria->limit = 9;
        $criteria->order = 'prid DESC';
        $items = ShopProduct::model()->findAll($criteria);
        $category = ShopNavbar::model()->findByPk($produc->cate_id);
        //$info = Page::model()->findByUrl('thongtinmuahang');
        //$info = isset($info) ? $info->content : '';
        $this->render('detail', array(
            'items' => $items,
            'produc' => $produc,
            //'info' => $info,
            'category' => $category,
        ));
    }

    public function actionTimkiem() {


        if (isset($_GET['cityID'])) {
            $cityID = $_GET['cityID'];
        } else {
            $cityID = Yii::app()->request->cookies['cityID']->value;
        }
        // $disID = $_POST['disID'];
        $key = '';
        if (isset($_POST['key'])) {
            $key = $_POST['key'];
        }
        //Yii::app()->request->cookies['cityID'] = new CHttpCookie('cityID', $cityID);
        $cookie = new CHttpCookie('cityID', $cityID);
        $cookie->expire = time() + 60 * 60 * 1;
        Yii::app()->request->cookies['$cityID'] = $cookie;
        // $category = $_POST['danhmuc'];
        //$chuoi ='chao mung ban den voi search '.$cityID.'/'.$disID.'/key='.$key;
        //$this->render('trang', array('chuoi' => $chuoi));
        $this->pageTitle = 'Tìm kiếm';

        $like = '';
        if (!empty($key)) {
            $like = ' and pr_name LIKE "%' . $key . '%"';
        }
        $city = '';
        if (!empty($cityID)) {
            $city = ' and ctiID =' . $cityID;
        }
        $district = '';
        if (!empty($disID)) {
            $district = ' and disID =' . $disID;
        }
        $danhmuc = '';
        if (!empty($category)) {
            $category1 = Category::model()->findAll(array(
                'condition' => 'flag=1 and parent= ' . $category,
            ));
            $danhmuc = ' AND cate_id in(' . $category;

            if (!empty($category1)) {
                foreach ($category1 as $item) {
                    $danhmuc = $danhmuc . ',' . $item->cateid;
                }
                $danhmuc = $danhmuc . ')';
            } else {
                $danhmuc = ' and cate_id =' . $category;
            }
        }
        $sql = 'flag=1 ' . $danhmuc . $city . $district . $like . ' order by `position` ASC';
        $criteria = new CDbCriteria();
        $criteria->condition = $sql;
        //$criteria->order = 'rank DESC,id DESC';

        $count = Product::model()->count($criteria);
        $pages = new CPagination($count);
        //results per page
        $pages->pageSize = 60;
        $pages->applyLimit($criteria);
        $produc = Product::model()->findAll($criteria);
        $result = 'Kết quả: tìm thấy ' . $count . ' địa điểm';
        $this->render('danh-muc', array(
            'produc' => $produc,
            'pages' => $pages,
            'result' => $result,
                //'sql' => $sql,
        ));
    }

    public function actionTimkiem1($id) {


        if (isset($_GET['id'])) {
            $cityID = $_GET['id'];
        } else {
            $cityID = Yii::app()->request->cookies['cityID']->value;
            echo $cityID;
        }
        //echo $cityID;
        // $disID = $_POST['disID'];
        //Yii::app()->request->cookies['cityID'] = new CHttpCookie('cityID', $cityID);
        $cookie = new CHttpCookie('cityID', $cityID);
        $cookie->expire = time() + 60 * 60 * 1;
        Yii::app()->request->cookies['$cityID'] = $cookie;
        $url = Yii::app()->createUrl('tim-dia-diem');
        $this->redirect($url);
    }

    public function actionXemPage($url) {

        $id = $_GET['id'];
        $shopcode = 'shop' . $id;
        if (isset(Yii::app()->request->cookies[$shopcode]->value)) {
            $shoparry = Yii::app()->request->cookies[$shopcode]->value;
            $savedCardArray = json_decode($shoparry, true);
        }
        $model= ShopPage::model()->findAll('shopid='.$savedCardArray[0].' and assia="'.$url.'"');

// if page is not found, then run a controller with that name
        if (empty($model)) {
            $this->render('page', array(
                'model' => $model
            ));
            throw new CHttpException(400, 'Khong tim thay trang');
        } else {
            foreach ($model as $item){
            $this->pageTitle = $item->title;
            Yii::app()->clientScript->registerMetaTag($item->keyword, 'keywords');
            Yii::app()->clientScript->registerMetaTag($item->deskiption, 'description');
            }
            $this->render('page', array(
                'model' => $model
            ));
        }
    }

}
